app.controller("gradeController",function($scope, $http){
    $http({
        method: 'GET',
        url: '/OA/grade/list'
    }).success(function (response) {
        console.log(response)               //总条数
        $scope.total = response.length;
        //反转函数转化abcd转dcba
        $scope.data = response.data;
        //选择显示的条数
        $scope.values = [{"limit":6}];
        //默认显示的条数
        $scope.selectedLimit=$scope.values[0];
        //默认显示当前页数
        $scope.currentPage = 1;
        if($scope.data != null){
            $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.currentPage-1),$scope.selectedLimit.limit*$scope.currentPage);
        }else{
            alert($scope.data);
        }
        $scope.page = {
            "limit":$scope.selectedLimit.limit,"pageSize":5,"pageNo":$scope.currentPage,"totalCount":$scope.total};
        // 2017 /7 /19
        // 定义一个空对象 , 用于更新和保存数据时临时存储
        $scope.prod = {};
        $scope.arrs=response.data;
        console.log($scope.arrs)
        // 定义一个单击删除按钮时触发的事件，用于删除选中行
        $scope.delete = function($index){
            if ($index>=0) {
                if(confirm("是否删除"+$scope.arrs[$index+(($scope.page.pageNo-1)*$scope.page.limit)].name+"年级") ){
                    //将需要删除的卡号传给后台  2017/7/10
                    $http({
                        method:"POST",
                        url:"/OA/grade/delete",
                        data:$.param({id:$scope.arrs[$index+(($scope.page.pageNo-1)*$scope.page.limit)].id}),
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                    })
                        .success(function (result) {
                            $scope.arrs.splice($index,1);
                            if (result.ok==true) {
                                alert(result.ok)
                            }
                        }).error(function(result){
                        alert(result.error)
                    })
                }
            };
        }
    })
    $scope.change = function(selectedLimit){
        $scope.page.limit = selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.pageChanged = function(){
        $scope.page.limit = $scope.selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.setPage = function (go) {
        $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
        console.log($scope.length);
        if(go > $scope.length){
            $scope.page.pageNo =  $scope.length;
            console.log($scope.length);

        }else{
            $scope.page.pageNo=go;
        }
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    };
    // 添加保存
    $scope.save=function () {
        var name=$("#gradename").val();
        $http({
            method:"POST",
            url:"/OA/grade/create",
            data:$.param({name:name}),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            if(response.ok==true){
                $("#studentadd").modal('hide');
                setTimeout(function () {
                    window.location.reload();
                },500)
            }
        })
    }
    });


